﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Config;
using log4net.Appender;
using System.Reflection;
using System.Diagnostics;

namespace NETCommon.Files
{
    public enum LogLevelL4N
    {
        DEBUG = 1,
        ERROR,
        FATAL,
        INFO,
        WARN
    }

    public static class NLogger
    {
        #region Members
        private static readonly ILog logger;
        #endregion

        #region Constructors
        static NLogger()
        {
            string currentFileName = System.IO.Path.GetFileNameWithoutExtension(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
            log4net.GlobalContext.Properties["LogName"] = string.Format("Log\\{0}", currentFileName);
            XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo("Config\\NETCommonLog.config"));
            logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        }
        #endregion

        #region Methods

        public static void WriteLog(LogLevelL4N logLevel, String log)
        {
            switch (logLevel)
            {
                case LogLevelL4N.DEBUG:
                    logger.Debug(log);
                    break;
                case LogLevelL4N.ERROR:
                    logger.Error(log);
                    break;
                case LogLevelL4N.FATAL:
                    logger.Fatal(log);
                    break;
                case LogLevelL4N.INFO:
                    logger.Info(log);
                    break;
                case LogLevelL4N.WARN:
                    logger.Warn(log);
                    break;
                default:
                    break;
            }
        }
        public static void ShowLog()
        {
            if (!String.IsNullOrEmpty(log4net.GlobalContext.Properties["LogName"].ToString()))
            {
                Process notePad = new Process();
                notePad.StartInfo.FileName = "notepad.exe";
                notePad.StartInfo.Arguments = NSimpleLog.GetFileName();
                notePad.Start();
            }
        }
        #endregion

    }
}
